"""
将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数：
输入: s = "PAYPALISHIRING", numRows = 4
输出: "PINALSIGYAHRPI"
解释:

P     I    N
A   L S  I G
Y A   H R
P     I

第一行和最后一行做特殊处理，中间行要处理临近的值一次 相距numRows-row*2-1 一次 是 row*2-1
"""


class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows <= 1 or numRows >= len(s) : return s
        res = ""
        for row in range(numRows):
            i = row
            flag = -1
            while i < len(s):
                res += s[i]
                if row == 0 or row == numRows - 1:
                    i += numRows*2 - 2
                    continue
                if flag == 1:
                    i += (row + 1)*2 - 2
                else:
                    i += (numRows - row)*2 - 2
                flag *= -1

        print(res)
s=Solution()
s.convert("PAYPALISHIRING",4)